package com.sunwayhorizo.health.business.clinic.mqa.query;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.Stateless;

import org.jboss.seam.annotations.Name;

import com.sunwayhorizo.health.business.base.query.BaseQuery;
import com.sunwayhorizo.health.common.StrUtil;
import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.rim.Act;
import com.sunwayhorizo.health.hl7.vocabulary.ActClass;
import com.sunwayhorizo.health.hl7.vocabulary.ActMood;
import com.sunwayhorizo.health.hl7.vocabulary.ParticipationType;

@Name("QADefectQuery")
@Stateless
public class QADefectQuery extends BaseQuery implements QADefectQueryLocal
{

    @SuppressWarnings("unchecked")
    public List<Act> findDefectInfoListByCond(String clinicDoctorRoleId, String clinicWorkGroupId, String mqaDoctorRoleId, String patientRoleId,
        int pageNo, int pageSize) throws Hl7Exception
    {
        conditions.clear();

        Map<String, Object> act = new HashMap<String, Object>();
        act.put("classCode", ActClass.INFO.code().toString());
        act.put("moodCode", ActMood.DEF.code().toString());
        if (StrUtil.isNotAllEmpty(clinicDoctorRoleId,clinicWorkGroupId) )
        {
            act.put("statusCode", "0");
        }
        conditions.put("act", act);

        Map<String, Object> part = new HashMap<String, Object>();
        if (StrUtil.isNotEmpty(clinicDoctorRoleId))
        {
            part.put("typeCode", ParticipationType.INF.code().stringValue());
        }
        if (StrUtil.isNotEmpty(clinicWorkGroupId))
        {
            part.put("typeCode", ParticipationType.INF.code().stringValue());
        }
        if (StrUtil.isNotEmpty(mqaDoctorRoleId))
        {
            part.put("typeCode", ParticipationType.CSF.code().stringValue());
        }
        if (StrUtil.isNotEmpty(patientRoleId))
        {
            part.put("typeCode", ParticipationType.SBJ.code().stringValue());
        }
        conditions.put("part", part);

        Map<String, Object> r = new HashMap<String, Object>();
        if (StrUtil.isNotEmpty(mqaDoctorRoleId))
        {
            r.put("roleId", mqaDoctorRoleId);
        }
        if (StrUtil.isNotEmpty(clinicDoctorRoleId))
        {
            r.put("roleId", clinicDoctorRoleId);
        }
        if (StrUtil.isNotEmpty(patientRoleId))
        {
            r.put("roleId", patientRoleId);
        }
        conditions.put("r", r);

        Map<String, Object> link = new HashMap<String, Object>();
        if (StrUtil.isNotEmpty(clinicWorkGroupId))
        {
            link.put("typeCode", "CONT");
        }
        conditions.put("link", link);

        Map<String, Object> tar = new HashMap<String, Object>();
        if (StrUtil.isNotEmpty(clinicWorkGroupId))
        {
            tar.put("roleId", clinicWorkGroupId);
        }
        conditions.put("tar", tar);

        Map<String, Object> orderBy = new HashMap<String, Object>();
        orderBy.put("asc", "act.actId");
        conditions.put("order by", orderBy);
        
        List<Act> infoMationList = (List<Act>) queryResults(conditions, Act.class, pageNo, pageSize);

        return infoMationList;
    }
    
    public Act findInformationByActId(String actId) throws Hl7Exception
    {
        Act information = (Act)findActByActId(Long.valueOf(actId));
        return information;
    }

    @Override
    public Long findCountDefectInfoListByCond(String clinicDoctorRoleId, String clinicWorkGroupId, String mqaDoctorRoleId, String patientRoleId)
        throws Hl7Exception
    {
        conditions.clear();

        Map<String, Object> act = new HashMap<String, Object>();
        act.put("classCode", ActClass.INFO.code().toString());
        act.put("moodCode", ActMood.DEF.code().toString());
        if (StrUtil.isNotAllEmpty(clinicDoctorRoleId,clinicWorkGroupId) )
        {
            act.put("statusCode", "0");
        }
        conditions.put("act", act);

        Map<String, Object> part = new HashMap<String, Object>();
        if (StrUtil.isNotEmpty(clinicDoctorRoleId))
        {
            part.put("typeCode", ParticipationType.INF.code().stringValue());
        }
        if (StrUtil.isNotEmpty(clinicWorkGroupId))
        {
            part.put("typeCode", ParticipationType.INF.code().stringValue());
        }
        if (StrUtil.isNotEmpty(mqaDoctorRoleId))
        {
            part.put("typeCode", ParticipationType.CSF.code().stringValue());
        }
        if (StrUtil.isNotEmpty(patientRoleId))
        {
            part.put("typeCode", ParticipationType.SBJ.code().stringValue());
        }
        conditions.put("part", part);

        Map<String, Object> r = new HashMap<String, Object>();
        if (StrUtil.isNotEmpty(mqaDoctorRoleId))
        {
            r.put("roleId", mqaDoctorRoleId);
        }
        if (StrUtil.isNotEmpty(clinicDoctorRoleId))
        {
            r.put("roleId", clinicDoctorRoleId);
        }
        if (StrUtil.isNotEmpty(patientRoleId))
        {
            r.put("roleId", patientRoleId);
        }
        conditions.put("r", r);

        Map<String, Object> link = new HashMap<String, Object>();
        if (StrUtil.isNotEmpty(clinicWorkGroupId))
        {
            link.put("typeCode", "CONT");
        }
        conditions.put("link", link);

        Map<String, Object> tar = new HashMap<String, Object>();
        if (StrUtil.isNotEmpty(clinicWorkGroupId))
        {
            tar.put("roleId", clinicWorkGroupId);
        }
        conditions.put("tar", tar);
        Long infos = queryCountResults(conditions, Act.class );

        return infos;
    }
}
